package com.youi.service.impl;

import com.youi.common.DelStatus;
import com.youi.dao.MarketThemeDao;
import com.youi.entity.MarketTheme;
import com.youi.entity.MarketThemeGoods;
import com.youi.entity.Operationlog;
import com.youi.entity.Sysuser;
import com.youi.service.MarketThemeGoodsService;
import com.youi.service.MarketThemeService;
import com.youi.service.OperationlogService;
import com.youi.util.CommonUtil;
import com.youi.util.DateUtil;
import com.youi.util.IpUtil;
import com.youi.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class MarketThemeServiceImpl extends BaseServiceImpl<MarketTheme> implements MarketThemeService {

    private MarketThemeDao marketThemeDao;
    @Autowired
    private OperationlogService operationlogService;
    @Autowired
    private MarketThemeGoodsService marketThemeGoodsService;

    @Autowired
    public void setfaultRecordDao(MarketThemeDao marketThemeDao) {
        this.marketThemeDao = marketThemeDao;
        super.setBaseDAO(marketThemeDao);
    }


    @Override
    public MarketTheme getByCode(String code) {
        return marketThemeDao.getByCode(code);
    }

    @Override
    public Map<String, String> saveOrEditMarketTheme(Long id, MarketTheme marketTheme, String ip, Sysuser sysuser, String themeName, Date gmtBegin, Date gmtEnd, String headers, String goods, String advertisements) {
        Map<String, String> result = new HashMap();
        //进行基础表的编辑
        if (id != null) { //修改
//                marketTheme = marketThemeService.get(id);
//                marketTheme.setGmtModify(new Date());
        } else { //新增，进行初始化
            marketTheme = new MarketTheme();
            marketTheme.setDelStatus(DelStatus.NODEL);
            marketTheme.setGmtCreated(new Date());
            marketTheme.setStatus(0);
            marketTheme.setPosition(0);
            marketTheme.setCreator(sysuser.getId());
            Integer count = marketThemeDao.getCount(null);
            marketTheme.setCode(String.valueOf(1001 + count));
        }
        marketTheme.setGmtModify(new Date());
        marketTheme.setName(themeName);
        marketTheme.setGmtBegin(gmtBegin);
        marketTheme.setGmtEnd(gmtEnd);
        marketThemeDao.saveOrUpdate(marketTheme);
        marketTheme = marketThemeDao.getByCode(marketTheme.getCode());

        //删除专题下的版头、商品、广告
        marketThemeGoodsService.updateDelStatusByThemeId(1, marketTheme.getId());

        //进行头版模块列表的编辑
        if (headers != null && headers.length() > 0) {
            List<MarketThemeGoods> marketThemeGoodList = JsonUtil.fromArrayJson2List(headers, MarketThemeGoods.class);
            if (marketThemeGoodList != null && marketThemeGoodList.size() > 0) {
                for (MarketThemeGoods marketThemeGoods : marketThemeGoodList) {
                    if (marketThemeGoods.getImgUrl() == null) {
                        result.put("code", "-1");
                        result.put("msg", "版头图片不能为空");
                        return result;
                    }
                    if (marketThemeGoods.getLinkType() != null) {
                        if (marketThemeGoods.getLinkType() == 1) {
                            if (marketThemeGoods.getGoodsId() == null) {
                                result.put("code", "-1");
                                result.put("msg", "版头商品不能为空");
                                return result;
                            }
                        } else if (marketThemeGoods.getLinkType() == 2) {
                            if (marketThemeGoods.getUrl() == null) {
                                result.put("code", "-1");
                                result.put("msg", "版头跳转地址不能为空");
                                return result;
                            } else {
                                Map<String, Object> urlParams = CommonUtil.parseUrl(marketThemeGoods.getUrl());
                                if (urlParams.get("id") != null) {
                                    marketThemeGoods.setToThemeId(Long.valueOf(String.valueOf(urlParams.get("id"))));
                                } else {
                                    marketThemeGoods.setToThemeId(Long.valueOf(-1));
                                }
                            }
                        }
                    } else {
                        result.put("code", "-1");
                        result.put("msg", "跳转类型不能为空");
                        return result;
                    }

                    marketThemeGoods.setDelStatus(0);
                    marketThemeGoods.setGmtCreated(new Date());
                    marketThemeGoods.setGmtModify(new Date());
                    marketThemeGoods.setGoodsPosition(1);
                    marketThemeGoods.setThemeId(marketTheme.getId());
                }
                marketThemeGoodsService.saveOrUpdateAll(marketThemeGoodList);
            }
        }

        //进行商品模块列表的编辑
        if (goods != null && goods.length() > 0) {
            List<MarketThemeGoods> goodsList = JsonUtil.fromArrayJson2List(goods, MarketThemeGoods.class);
            if (goodsList != null && goodsList.size() > 0) {
                for (MarketThemeGoods marketThemeGoods : goodsList) {
                    if (marketThemeGoods.getGoodsId() == null) {
                        result.put("code", "-1");
                        result.put("msg", "商品不能为空");
                        return result;
                    }
                    marketThemeGoods.setDelStatus(0);
                    marketThemeGoods.setGmtCreated(new Date());
                    marketThemeGoods.setGmtModify(new Date());
                    marketThemeGoods.setGoodsPosition(2);
                    marketThemeGoods.setLinkType(1);
                    marketThemeGoods.setThemeId(marketTheme.getId());
                }
                marketThemeGoodsService.saveOrUpdateAll(goodsList);
                marketTheme.setGoodsCount(goodsList.size());
            } else {
                marketTheme.setGoodsCount(0);
            }
        } else {
            marketTheme.setGoodsCount(0);
        }
        //更新url和商品数量
        marketTheme.setUrl("/pages/theme/theme?id=" + marketTheme.getId());
        marketThemeDao.saveOrUpdate(marketTheme);

        //进行广告栏位模块列表的编辑
        if (advertisements != null && advertisements.length() > 0) {
            List<MarketThemeGoods> advertisementsList = JsonUtil.fromArrayJson2List(advertisements, MarketThemeGoods.class);
            if (advertisementsList != null && advertisementsList.size() > 0) {
                for (MarketThemeGoods marketThemeGoods : advertisementsList) {

                    if (marketThemeGoods.getImgUrl() == null) {
                        result.put("code", "-1");
                        result.put("msg", "广告图片不能为空");
                        return result;
                    }
                    if (marketThemeGoods.getLinkType() != null) {
                        if (marketThemeGoods.getLinkType() == 1) {
                            if (marketThemeGoods.getGoodsId() == null) {
                                result.put("code", "-1");
                                result.put("msg", "广告商品不能为空");
                                return result;
                            }
                        } else if (marketThemeGoods.getLinkType() == 2) {
                            if (marketThemeGoods.getUrl() == null) {
                                result.put("code", "-1");
                                result.put("msg", "广告跳转地址不能为空");
                                return result;
                            } else {
                                Map<String, Object> urlParams = CommonUtil.parseUrl(marketThemeGoods.getUrl());
                                if (urlParams.get("id") != null) {
                                    marketThemeGoods.setToThemeId(Long.valueOf(String.valueOf(urlParams.get("id"))));
                                } else {
                                    marketThemeGoods.setToThemeId(Long.valueOf(-1));
                                }
                            }
                        }
                    } else {
                        result.put("code", "-1");
                        result.put("msg", "跳转类型不能为空");
                        return result;
                    }
                    marketThemeGoods.setDelStatus(0);
                    marketThemeGoods.setGmtCreated(new Date());
                    marketThemeGoods.setGmtModify(new Date());
                    marketThemeGoods.setGoodsPosition(3);
                    marketThemeGoods.setThemeId(marketTheme.getId());
                }
                marketThemeGoodsService.saveOrUpdateAll(advertisementsList);

            }
        }

        //操作记录

        if (id == null) {
            operationlogService.saveOrUpdate(new Operationlog(sysuser.getId(), ip, IpUtil.getAddresses(ip), "添加了专题：" + marketTheme.getName()));
        } else {
            operationlogService.saveOrUpdate(new Operationlog(sysuser.getId(), ip, IpUtil.getAddresses(ip), "修改了专题：" + marketTheme.getName()));
        }
        result.put("code", "1");
        result.put("msg", "成功");
        return result;
    }

    @Override
    public void updateThemeEndedStatus() {
        Map<String, Object> sParams = new HashMap();
        sParams.put("status", Arrays.asList(0, 1, 2));
        Map<String, Object> ltGmt = new HashMap();
        ltGmt.put("gmtEnd", DateUtil.simpdfyMdHms.format(new Date()));
        List<MarketTheme> endingThemes = marketThemeDao.getByParamsList(sParams, null, null, null, null, null, ltGmt);
        if (endingThemes.size() > 0) {
            for (MarketTheme marketTheme : endingThemes) {
                marketTheme.setStatus(3);
            }
            marketThemeDao.saveOrUpdateAll(endingThemes);
        }
    }
}
